package com.kly.module_base.extension

import android.app.Application
import android.os.Process
import android.text.TextUtils
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
import java.io.IOException

/**
 * 判断是否主进程
 */
fun Application.isMainProcess(): Boolean {
    val mainProcessName = this.applicationInfo.processName
    val currentProcessName = getCurrentProcessName()
    return TextUtils.equals(mainProcessName, currentProcessName)
}

/**
 * 获取当前进程名称
 */
private fun Application.getCurrentProcessName(): String {
    var reader: BufferedReader? = null
    return try {
        val file = File("/proc/" + Process.myPid() + "/" + "cmdline")
        reader = BufferedReader(FileReader(file))
        reader.readLine().trim { it <= ' ' }
    } catch (e: Exception) {
        ""
    } finally {
        if (reader != null) {
            try {
                reader.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }
}










